package com.mlk.rbac.Service.impl;

import java.util.List;

import javax.annotation.Resource;

import com.mlk.core.component.page.Page;
import com.mlk.core.component.page.Pager;
import com.mlk.core.constants.Constant;
import com.mlk.core.util.DictUtils;
import com.mlk.core.util.ServerResponse;
import com.mlk.rbac.Service.SysDictTypeService;
import com.mlk.rbac.dto.SysDictDTO;
import com.mlk.rbac.mapper.SysDictMapper;
import com.mlk.rbac.mapper.SysDictTypeMapper;
import com.mlk.rbac.po.SysDict;
import com.mlk.rbac.po.SysDictType;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import org.springframework.transaction.annotation.Transactional;


/**
 * 数据字典类型服务
 * @author trekxu
 */
@Service
public class SysDictTypeServiceImpl implements SysDictTypeService {

	@Resource
	private SysDictTypeMapper sysDictTypeMapper;

	@Resource
	private  SysDictMapper sysDictMapper;

	@Resource
	private RedisTemplate<String,String> redisTemplate;

	/**
	 * 其中 type = 1 表示查询可用
	 * 	   type = 0 表示查询不可用
	 *     不传 表示查询全部
	 */
	@Override
	@Page
	public Pager<SysDictType> dictTypePageList(Integer type,SysDictType dictType) {
		if(type != null){
			if(type == 1) {
				dictType.setStatus(Constant.status_ok);
			}else if(type == 0) {
				dictType.setStatus(Constant.status_fail);
			}else{
				return Pager.noData("传入的参数类型type不正确");
			}
		}

		return Pager.<SysDictType>adapt(this.sysDictTypeMapper.selectByList(dictType));

	}
	
	@Override
	@Transactional
	public ServerResponse dictTypeAdd(SysDictType dictType) {
		if(StringUtils.isBlank(dictType.getTypeName())) {
			return ServerResponse.fail("数据类型参数的名称不能为空");
		}
		
		//判断是否存在相同的字典类型
		int count = this.sysDictTypeMapper.selectCount(new EntityWrapper<SysDictType>(SysDictType.builder().typeDesc(dictType.getTypeDesc()).build()));
		if(count != 0) {
			return ServerResponse.fail("已经存在相同标识符的字典类型");
		}
		
		SysDictType sysDictType = SysDictType
					.builder()
					.typeName(dictType.getTypeName())
					.typeDesc(dictType.getTypeDesc())
					.status(Constant.status_ok)
					.build();
		
		count = this.sysDictTypeMapper.insert(sysDictType);
		if(count == 0) {
			return ServerResponse.fail("添加字典类型失败");
		}
		return ServerResponse.ok("添加字典类型成功");
	}
	
	@Override
	@Transactional
	public ServerResponse dictTypeOnOrOff(Integer id) {
		
		if(id ==null) {
			return ServerResponse.fail("被操作的字典类型不能为空");
		}
		
		SysDictType dictType = this.sysDictTypeMapper.selectById(id);
		if(dictType == null) {
			return ServerResponse.fail("字典不存在");
		}
		
		//更新
		int status = Constant.ConvertStatus(dictType.getStatus());
		int count = this.sysDictTypeMapper.updateById(SysDictType.builder().id(id).status(status).build());
		if(count == 0) {
			return ServerResponse.fail("冻结或激活字典类型失败");
		}
		return ServerResponse.ok("冻结或激活字典类型成功");
	}
	
	@Override
	@Transactional
	public ServerResponse dictTypeUpdate(Integer id,String typeName) {
		if(id ==null) {
			return ServerResponse.fail("被操作的字典类型不能为空");
		}
		
		int count = this.sysDictTypeMapper.selectCount(new EntityWrapper<SysDictType>(SysDictType.builder().typeName(typeName).build()));
		if(count != 0) {
			return ServerResponse.fail("已经存在相同名称的字典类型");
		}
		
		// 现在更新数据字典类型的名称
		SysDictType dictType = SysDictType.builder().id(id).typeName(typeName).build();
		count = this.sysDictTypeMapper.updateById(dictType);
		if(count == 0) {
			return ServerResponse.fail("更新数据字典类型失败");
		}
		// 此时更新字典缓存中的内容
        SysDictType sysDictType = this.sysDictTypeMapper.selectById(id);
        List<SysDictDTO> items = sysDictMapper.queryAllDictItem(sysDictType.getTypeDesc());
        DictUtils.refreshCache(items);
		return ServerResponse.ok("更新数据字典类型成功");
	}
	
	@Override
	public List<SysDictType> dictTypeList(Integer type) {
		if(type == null) {
			return this.sysDictTypeMapper.selectList(null);
		}
		
		if(type == 1) {
			return this.sysDictTypeMapper.selectList(new EntityWrapper<>(SysDictType.builder().status(Constant.status_ok).build()));
		}
		if(type == 0) {
			return this.sysDictTypeMapper.selectList(new EntityWrapper<>(SysDictType.builder().status(Constant.status_fail).build()));
		}
		
		return null;
	}


    @Override
    public ServerResponse findDictTypeByDesc(String typeDesc) {
        SysDictType dictType = this.sysDictTypeMapper.selectOne(SysDictType.builder().typeDesc(typeDesc).build());
        List<SysDict> dicts = this.sysDictMapper.selectList(new EntityWrapper(SysDict.builder().dictType(dictType.getId()).build()));
        return ServerResponse.ok(dicts);
    }

}
